﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Common;
using System.Data;

namespace map.taobao
{
    public partial class pointItem : System.Web.UI.Page
    {         

        #region 页面载入
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (validateRequest())
                {
                    this.AreaHyperLink.Text = Request["area"];
                    this.AreaHyperLink.NavigateUrl = ( "~/search/areadetail.aspx?id=" + Request.QueryString["id"]);
                    BindWebPager();
                }
            }
        }
        #endregion

        #region 辅助方法
        private void BindWebPager()
        {
            if (validateRequest())
            {
                string sortingType = "";
                string selectedValue = this.SortTypeDropDownList.SelectedValue;
                switch (selectedValue)
                {
                    case "PEsc":
                        {
                            sortingType = "1";
                            break;
                        }
                    case "PDes":
                        {
                            sortingType = "2";
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }


                string sqlText = "exec [searchpointitem] @clat=" + Request["lat"] + ", @clong=" + Request["lng"];
                sqlText += ", @keywordtext='" + Request["searchKeys"] + "'";
                sqlText += ", @keyword='" +zfunction .ReplaceKeyWord ( Request["searchKeys"])+"'";
                sqlText += ", @pricefrom=" + Request["lowPrice"];
                sqlText += ", @priceto=" + Request["highPrice"];
                sqlText += ", @rsms='" + Request["rsmsValue"]+"'";
                sqlText += ", @qtth='" + Request["qtthValue"] + "'";
                sqlText += ", @stwx='" + Request["shwxValue"] + "'";
                sqlText += ", @sdfh='" + Request["sdfhValue"] + "'";
                sqlText += ", @cataid='" + Request["currentCate"] + "'";
                if (sortingType != "" )
                {
                    sqlText += ", @orderType='" + sortingType + "'";
                }
                DataSet dSet = SqlHelper.Instance.ExecuteQuery(sqlText);
                if (dSet.Tables.Count > 0)
                {
                    int count = dSet.Tables[0].Rows.Count;
                    this.TotalCountLabel.Text = count.ToString() + " 个宝贝符合要求";
                    this.MainWebPager.DataSource = dSet.Tables[0];
                    this.MainWebPager.DataBind();
                }
            }
        }

        private bool validateRequest()
        {
            return Request["lat"] != null && Request["lng"] != null && Request["area"] != null;
        }
        #endregion

        #region 事件
        protected void MainWebPager_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            this.MainWebPager.CurrentPageIndex = e.NewPageIndex;
            //this.MainWebPager.DataBind();
            BindWebPager();
        }
       

        protected void SortTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.SortTypeDropDownList .SelectedValue !="")
                BindWebPager(); 
        }
        #endregion
    }
}
